<?php

class Categories extends Database
{
	protected $id;	
	protected $categoryname;
	
	function Categories($id, $categoryname)
	{
        parent::__construct();
		$this->id=$id;		
		$this->categoryname=$categoryname;
	}
	
	function printCategoriesGuest($res)
	{
		print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=600><b>Назва категорії</b></td>
		  <td width=100><b>Кількість</b></td>	  
		 </tr>
		</table>
		");	
		while ($row = mysql_fetch_array($res))
		{
			$sql=mysql_query("SELECT COUNT(*) FROM products WHERE category_id = '$row[0]'");
			$sql=mysql_fetch_array($sql);
			print("<br>
			<table align=center border=1 cellpadding=5 cellspacing=0>	
			<tr>
			");
			print("
			 <td width=600 height=35>
			  <a href=/index.php?page=products&category=".$row[0]." id=categorylink>".$row[1]."</a>
			 </td>
			 <td width=100>
			  ".$sql[0]."
			 </td>
			 ");
			print("</tr>");
			print("</table>");
		}	
		print("<p align=center><br>");				
	}

	function printCategoriesAdmin($res)
	{
		print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=600><b>Назва категорії</b></td>
		  <td width=100><b>Кількість</b></td>	  
		 </tr>
		</table>
		");	
		while ($row = mysql_fetch_array($res))
		{
			$sql=mysql_query("SELECT COUNT(*) FROM products WHERE category_id = '$row[0]'");
			$sql=mysql_fetch_array($sql);
			print("<br>
			<table align=center border=1 cellpadding=5 cellspacing=0>
			<tr>
			");
			print("
			 <td width=600 height=35 colspan=2>
			  <a href=/index.php?page=products&category=".$row[0]." id=categorylink>".$row[1]."</a>
			 </td>
			 <td width=100 rowspan=2>
			  ".$sql[0]."
			 </td>
			 ");
			print("</tr>");
			print("<tr>");
			print("
			 <td height=15>
			 	<a href=/index.php?page=category_edit&category=".$row[0].">Редагувати</a>
			 </td>
			 <td height=15>
			 	<a href=/index.php?page=category_delete&category=".$row[0].">Видалити</a>
			 </td>
			 ");
			print("</tr>");
			print("</table>");
		}	
		print("<p align=center><br>");				
	}

    function printCategoryName()
    {
        $sql=mysql_query("SELECT categoryname FROM categories WHERE id='$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

    function existingAdd()
    {
        $name=mysql_real_escape_string($this->categoryname);
        $sql=mysql_query("SELECT COUNT(*) FROM categories WHERE categoryname= '$name'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function existingEdit()
    {
        $name=mysql_real_escape_string($this->categoryname);
        $sql=mysql_query("SELECT COUNT(*) FROM categories WHERE categoryname= '$name' AND id!='$this->id'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function countInCategory()
    {
        $sql=mysql_query("SELECT COUNT(*) FROM products WHERE category_id = '$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

	function add()
	{
		mysql_query("INSERT INTO categories (`categoryname`) VALUES ('$this->categoryname')");
	}
	
	function edit()
	{
		mysql_query("UPDATE categories SET `categoryname`='$this->categoryname' WHERE id='$this->id'");
	}

	function delete()
	{
		mysql_query("DELETE FROM categories WHERE id='$this->id'");
	}

	function show()
	{		
		$res = mysql_query("SELECT id, categoryname FROM categories ORDER BY categoryname");
		if ($_SESSION['login']=="admin") 
		{
			$this->printCategoriesAdmin($res);
		}
		else
		{
			$this->printCategoriesGuest($res);
		}
	}

}

?>